Documents > Cookbook >Manipulate TextSearch
TextDocument textdoc=(TextDocument)TextDocument.loadDocument("textsearch.odt");
TextNavigation search1;
search1=new TextNavigation("What",textdoc);
while (search1.hasNext()) {
TextSelection item1 = (TextSelection) search1.nextSelection();
System.out.println(item1);
}
TextNavigation search2=new TextNavigation("good",textdoc);
while(search2.hasNext()){
TextSelection item2=(TextSelection) search2.nextSelection();
String searchedText=item2.getText();
int searchedIndex=item2.getIndex();
System.out.println(searchedText);
System.out.println(searchedIndex);
}
search2=new TextNavigation("day",textdoc);
while(search2.hasNext()){
TextSelection item=(TextSelection) search2.nextSelection();
item.cut();
}
search2 = null;
search2 = new TextNavigation("good", textdoc);
TextSelection pastesource = null;
TextNavigation search3 = new TextNavigation("change", textdoc);
if (search3.hasNext()) {
pastesource = (TextSelection) search3.nextSelection();
}
while (search2.hasNext()) {
TextSelection item = (TextSelection) search2.nextSelection();
//paste "change" at the front of "good" pastesource.pasteAtFrontOf(item);
//paste "change" at the end of "good" pastesource.pasteAtEndOf(item);
}
search2 = null;
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith("replacedest");
}
search2 = null;
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
Paragraph paragraph = textdoc.getParagraphByIndex(0, true);
item.replaceWith(paragraph);
}
search2 = null;
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
//Use URI as parameter. item.replaceWith(new URI("image.png"));
}
//Use Image as parameter. item.replaceWith(Image.newImage(para, new URI("image.png")));
search2 = null;
Table table = textdoc.getTableByName("myTable");
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith(table);
}
search2 = null;
Field field = textdoc.getVariableFieldByName("myField");
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith(field);
}
search2 = null;
TextDocument destDoc=TextDocument.loadDocument("replacedest.odt");
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith(destDoc);
}
search2 = null;
search2 = new TextNavigation("network", textdoc);
while (search2.hasNext()) {
TextSelection item = (TextSelection) search2.nextSelection();
item.addHref(new URL("http://www.ibm.com"));
}
TextNavigation search4 = new TextNavigation("natwork", textdoc);
while (search4.hasNext()) {
TextSelection selection = (TextSelection) search4.nextSelection();
selection.addComment("Please change 'natwork' with 'network'.", "SpellChecker");
}
TextDocument doc = TextDocument.loadDocument("fieldSample.odt");
TextNavigation search = new TextNavigation("ReplaceDateTarget", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.nextSelection();
FieldSelection fieldSelection = new FieldSelection(item);
fieldSelection.replaceWithSimpleField(Field.FieldType.FIXED_DATE_FIELD);
}
TextSelection item = (TextSelection) search.nextSelection();
FieldSelection fieldSelection = new FieldSelection(item);
fieldSelection.replaceWithConditionField("test_con_variable == \"true\"", "trueText", "falseText");
fieldSelection.replaceWithHiddenTextField("test_con_variable == \"true\"", "hiddenText");
ReferenceField referenceField = Fields.createReferenceField(doc.addParagraph("span").getOdfElement(), "selection-test-ref");
fieldSelection.replaceWithReferenceField(referenceField, ReferenceField.DisplayType.TEXT);
VariableField userVariableField = Fields.createUserVariableField(doc, "selection_user_variable", "test");
fieldSelection.replaceWithVariableField(userVariableField);
TextStyleNavigation searchStyle1;
TreeMap<OdfStyleProperty, String> searchProps = new TreeMap<OdfStyleProperty, String>();
searchProps.put(StyleTextPropertiesElement.FontName, "Times New Roman1");
searchProps.put(StyleTextPropertiesElement.FontSize, "16pt");
searchStyle1 = new TextStyleNavigation(searchProps, textdoc);
if (searchStyle1.hasNext()) {
TextSelection itemstyle = (TextSelection) searchStyle1.nextSelection();
System.out.print((itemstyle.toString()));
}